<?php
function halt_js($str){
	echo"<script language='javascript'>alert('".addslashes($str)."');</script>";
	exit();
}

function reload_js($str,$url){
	if($str){
		echo"<script language='javascript'>alert('".addslashes($str)."');</script>";
	}
	if($url){
		echo"<script language='javascript'>window.location='".addslashes($url)."';</script>";
	}
}

function isLogin(){
	if($_SESSION['admin']==''){
		reload_js('','login.php');
	}
}

function strip_url($params){
	$querystring = $_SERVER['QUERY_STRING'];
	if($querystring){
		$parameters = explode("&",$querystring);
		$join=!$params['join']?'-':$params['join'];
	
		$newarr =array();
		foreach($parameters as $v){
			$arr=explode("=",$v);
			//$arr[0]=$arr[0]?$arr[0]:' ';
			//$params['exc']=$params['exc']?$params['exc']:'sfsx';
			if(!strstr($params['exc'],$arr[0])){
				$v=implode("-",$arr);
				array_push($newarr,$v);
			}
		}
		$newurl = implode($join,$newarr);
		if($newurl){$newurl.=$join;}
		
		return $newurl;
	}
}

function getremaiurl($cid){
	global $lcat,$ljoin;
	
	$surl=array('exc' => 'page,cid,imageField.x,imageField.y','join' => $ljoin);
	$url='';
	if(strip_url($surl)){$url='cid-'.$cid.$ljoin.strip_url($surl);}else{$url=$lcat.$cid.$ljoin;}
	return $url;
}
function getsearchurl($cid){
	global $lcat,$ljoin,$q,$liebiao;
	
	$surl=array('exc' => 'page,cid,q,liebiao,imageField.x,imageField.y','join' => $ljoin);
	$url='';
	if(strip_url($surl)){$url=$ljoin.'cid-'.$cid.$ljoin.strip_url($surl).'q-'.urlencode($q).$ljoin.'liebiao-'.$liebiao.$ljoin;}else{$url="-".urlencode($q).$ljoin.$lcat.$cid.$ljoin;}
	return $url;
}
function geturl($cid){
	global $lcat,$ljoin;
	
	$surl=array('exc' => 'page,cid,imageField.x,imageField.y','join' => $ljoin);
	$url='';
	if(strip_url($surl)){$url='cid-'.$cid.$ljoin.strip_url($surl);}else{$url="";}
	return $url;
}

function file_get($url){
	$contents=file_get_contents($url);
	if($contents){
		return $contents;
	}elseif(function_exists("curl_init")){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
		$contents = curl_exec($ch);
		curl_close($ch);
		return $contents;
	}
}

function autoExecute($table, $arrFields, $mode, $where=false){
	if($mode=='update'){
		$value="";
		foreach($arrFields as $k => $v){
			$value.=$k."='".$v."',";
		}
		$value=substr($value,0,strlen($value)-1);
		$sql="update ".$table." set ".$value." ".$where;
	}else if($mode=='insert'){
		$fields="";
		$value="";
		foreach($arrFields as $k => $v){
			$fields.=$k.",";
			$value.=$v.",";
		}
		$fields=substr($fields,0,strlen($fields)-1);
		$value=substr($value,0,strlen($value)-1);
		$sql="insert into ".$table." (".$fields.") values(".$value.")";
	}
	return mysql_query($sql);
}

function get_list_by_id($table,$t_id,$id){
	global $prefix;
	
	$sql="SELECT * FROM ".$prefix.$table." WHERE ".$t_id."=".$id;
	$result=mysql_query($sql);
	if($arr=mysql_fetch_array($result)){
		return $arr;
	}
}

function get_list_count($table,$sqladd){
	global $prefix;
	
	$sql="SELECT count(*) as c FROM ".$prefix.$table." ".$sqladd;
	$result=mysql_query($sql);
	if($arr=mysql_fetch_array($result)){
		return $arr['c'];
	}
}

function get_field_by_id($table,$field,$t_id,$id){
	global $prefix;
	
	$sql="SELECT ".$field." FROM ".$prefix.$table." WHERE ".$t_id."=".$id;
	$result=mysql_query($sql);
	if($arr=mysql_fetch_array($result)){
		return $arr;
	}
}

function urlstrip($strip){
	$newurl='';
	$arr=explode('&',$_SERVER['QUERY_STRING']);
	foreach($arr as $k => $v){
		if(!strstr($v,$strip)){
			$newurl.=$v."&";
		}
	}
	$newurl=substr($newurl,0,strlen($newurl)-1);
	return $newurl;
}

function pagefy($totle,$displaypg=20,$url=''){

	global $page,$firstcount,$pagenav,$_SERVER;
	$GLOBALS["displaypg"]=$displaypg;
	
	if(!$page) $page=1;
	if(!$url){ $url=$_SERVER["REQUEST_URI"];}
	$parse_url=parse_url($url);
	$url_query=$parse_url["query"]; //单独取出URL的查询字串
	if($url_query){
		$url_query=ereg_replace("(^|&)page=$page","",$url_query);//去掉URL中页码信息。
		$url=str_replace($parse_url["query"],$url_query,$url); 	//将处理后的URL的查询字串替换原来的URL的查询字串：
		if($url_query) $url.="&page"; else $url.="?page";
	}else{
		$url.="?page";
	}
	
	$lastpg=ceil($totle/$displaypg); //最后页，也是总页数
	$page=min($lastpg,$page);
	$prepg=$page-1; //上一页
	$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
	$firstcount=($page-1)*$displaypg;
	
	$pagenav="显示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 条记录，共 $totle 条记录<BR>";
	if($lastpg<=1) return false; //如果只有一页则跳出函数：
	$pagenav.=" <a href='$url=1'>首页</a> ";
	if($prepg) $pagenav.=" <a href='$url=$prepg'>前页</a> "; else $pagenav.=" 前页 ";
	if($nextpg) $pagenav.=" <a href='$url=$nextpg'>后页</a> "; else $pagenav.=" 后页 ";
	$pagenav.=" <a href='$url=$lastpg'>尾页</a> ";
	
	//下拉跳转列表，循环列出所有页码：
	$pagenav.="　到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
	for($i=1;$i<=$lastpg;$i++){
		if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
		else $pagenav.="<option value='$i'>$i</option>\n";
	}
	$pagenav.="</select> 页，共 $lastpg 页";
}

function   passport_decrypt($txt,$key){
	$txt = passport_key(base64_decode($txt), $key);
	$tmp = '';
	for($i = 0;  $i < strlen($txt); $i++){
		$tmp .=  $txt[$i] ^   $txt[++$i];
	}
	return   $tmp;
} 

function   passport_key($txt,   $encrypt_key){
	$encrypt_key   =   md5($encrypt_key);
	$ctr  =  0;
	$tmp  =  '';
	for($i = 0;$i <  strlen($txt);   $i++){
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
		$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
	}
	return   $tmp;
}

function passport_encrypt($txt, $key) {
	srand((double)microtime() * 1000000);
	$encrypt_key = md5(rand(0, 32000));
	$ctr = 0;
	$tmp = '';
	for($i = 0; $i < strlen($txt); $i++) {
		$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
		$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
	}
	return base64_encode(passport_key($tmp, $key));
}

function checkNotNull($arr,$url,$who){
	for($i=0;$i<count($arr);$i++){
		if($_POST[$arr[$i]]==''){reload_js($who."数据不能为空",$url);}
	}
}

function getUpdateSql($fields,$table,$sqladd,$mode=1){
	global $prefix;

	$values='';
	if($mode==1){
		for($i=0;$i<count($fields);$i++){
			if($i==0){
				$values.=$fields[$i]."='".$_POST[$fields[$i]]."'";
			}else{
				$values.=",".$fields[$i]."='".addslashes($_POST[$fields[$i]])."'";
			}
		}
	}elseif($mode==2){
		$i=0;
		foreach($fields as $k => $v){
			if($i==0){
				$values.=$k."='".$v."'";
			}else{
				$values.=",".$k."='".addslashes($v)."'";
			}
			$i++;
		}
	}
	return "update ".$prefix.$table." set ".$values." ".$sqladd;
}

function getInsertSql($fields,$table,$mode=1){
	global $prefix;

	$cols='';
	$values='';
	if($mode==1){
		for($i=0;$i<count($fields);$i++){
			if($i==0){
				$cols.=$fields[$i];
				$values.="'".$_POST[$fields[$i]]."'";
			}else{
				$cols.=",".$fields[$i];
				$values.=",'".addslashes($_POST[$fields[$i]])."'";
			}
		}
	}elseif($mode==2){
		$i=0;
		foreach($fields as $k => $v){
			if($i==0){
				$cols.=$k;
				$values.="'".$v."'";
			}else{
				$cols.=",".$k;
				$values.=",'".addslashes($v)."'";
			}
			$i++;
		}
	}
	return "insert into ".$prefix.$table." (".$cols.") values (".$values.")";
}


function mysubstr($string,$length,$append = false){ 
	if (strlen($string) <= $length ){ 
		return $string; 
	}else{ 
		$i = 0; 
		while ($i < $length){ 
			$stringtmp = substr($string,$i,1); 
			if ( ord($stringtmp) >=224 ){ 
				$stringtmp = substr($string,$i,3); 
				$i = $i + 3; 
			}elseif( ord($stringtmp) >=192 ){ 
				$stringtmp = substr($string,$i,2); 
				$i = $i + 2; 
			}else{ 
				$i = $i + 1; 
			} 
			$stringlast[] = $stringtmp; 
		} 
		$stringlast = implode("",$stringlast); 
		if($append){$stringlast .= "...";} 
		return $stringlast; 
	} 
}
?>